package com.sikaryofficial.workbench.api;

import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import com.sikaryofficial.workbench.constant.OrderHeaderStatusEnum;
import com.sikaryofficial.workbench.domain.dto.req.order.GoodsOrderReq;
import com.sikaryofficial.workbench.domain.dto.req.order.OrderDataSyncReq;
import com.sikaryofficial.workbench.manager.OrderManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order/api")
@Api(tags = "订单管理-外部接口")
@Slf4j
public class OrderApi {

    @Resource
    private OrderManager orderManager;

    /**
     *
     * @param orderHeaderId 订单头ID
     * @param processStatus 处理状态
     * @param processItem   处理项
     * @return
     */
    @GetMapping("/updateOrderProcessStatus")
    @ApiOperation(value = "更新订单流程状态")
    public R<Boolean> updateOrderProcessStatus(Long orderHeaderId, Integer processStatus, Integer processItem) {
        return R.ok(orderManager.updateOrderProcessStatus(orderHeaderId, processStatus, processItem));
    }

    /**
     * 更新订单状态
     * @param orderHeaderId 主订单ID
     * @param status    状态
     * @return
     */
    @GetMapping("/updateOrderStatus")
    @ApiOperation(value = "更新订单状态")
    public R<Boolean> updateOrderStatus(Long orderHeaderId, Integer status) {
        OrderHeaderStatusEnum statusEnum = OrderHeaderStatusEnum.getOrderHeaderStatusByCode(status);
        if (statusEnum == null) {
            return R.fail("订单状态不存在");
        }
        log.info("updateOrderStatus: orderHeaderId:{}, status:{}", orderHeaderId, status);
        orderManager.updateOrderStatus(orderHeaderId, statusEnum);
        return R.ok(true);
    }

    @GetMapping("/updateEstimateArrivalTime")
    @ApiOperation(value = "更新预计到货时间")
    public R<Boolean> updateEstimateArrivalTime(@RequestParam("orderHeaderId") Long orderHeaderId,
                                                @RequestParam("arrivalTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date arrivalTime) {
        log.info("updateEstimateArrivalTime: orderHeaderId:{}, arrivalTime:{}", orderHeaderId, arrivalTime);
        return R.ok(orderManager.updateEstimateArrivalTime(orderHeaderId, arrivalTime));
    }

    @ApiOperation(value = "报价通过后订单数据同步")
    @PostMapping("/syncOrderInfo")
    public R<Boolean> syncOrderInfo(@RequestBody OrderDataSyncReq orderHeaderReq) {
        return R.ok(orderManager.syncOrderInfo(orderHeaderReq));
    }

    @Log(title = "内网销售代客下单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "内网销售代客下单")
    @PostMapping("/helpCustomerPlaceOrder")
    public R<Void> helpCustomerPlaceOrder(@RequestBody List<GoodsOrderReq> goodsList) {
        orderManager.helpCustomerPlaceOrder(goodsList);
        return R.ok();
    }

    @Log(title = "内网销售下定制订单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "定制下单")
    @PostMapping("/placeCustomOrder")
    public R<Void> placeCustomOrder(@RequestBody List<GoodsOrderReq> goodsList) {
        orderManager.placeCustomOrder(goodsList);
        return R.ok();
    }
}
